package learn.advance.lambda1;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;

import javax.crypto.spec.PSource;
import java.util.Arrays;
import java.util.Comparator;

class Demo2Lambda {
    public static void Test() {
        //使用匿名内部类对象,实现多线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();

        //使用Lambda表达式,实现多线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "lambda");
        }).start();

        new Thread(() -> System.out.println(Thread.currentThread().getName() + "优化lambda")).start();
    }
}

class Person {
    private String name_;
    private int age_;

    public Person() {

    }

    public Person(String name, int age) {
        this.name_ = name;
        this.age_ = age;
    }

    public int GetAge() {
        return age_;
    }

    @Override
    public String toString() {
        return name_ + ":" + age_ + " ";
    }
}

class Demo3Lambda {

    public static void Test() {
        Person[] arr = {new Person("小明", 18),
                new Person("小红", 20),
                new Person("小军", 15)};
        for (Person p : arr) {
            System.out.print(p);
        }
        System.out.println();

        Arrays.sort(arr, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.GetAge() - o2.GetAge();
            }
        });

        for (Person p : arr) {
            System.out.print(p);
        }
        System.out.println();

        Arrays.sort(arr, (Person o1, Person o2) -> {
            return o2.GetAge() - o1.GetAge();
        });

        for (Person p : arr) {
            System.out.print(p);
        }
        System.out.println();
    }


}


public class TestLambda {
    public static void main(String[] args) {
        // Demo2Lambda.Test();
        Demo3Lambda.Test();
    }
}
